﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Nop.Core.Log.Model;

namespace Nop.Core.Log.Service
{
    /// <summary>
    /// table storage service
    /// </summary>
    public class TableStorage : ITagbleStorage
    {
        /// <summary>
        /// add entity to table
        /// </summary>
        /// <param name="message">log message</param>
        /// <param name="assembely">call assembly name</param>
        /// <param name="level">log level</param>
        /// <param name="tableName">table name</param>
        public void Insert(string message, string assembely, Level level, string tableName)
        {
            var entity = ComposeEntity(message, assembely, level);

            var table = StorageTableClientManager.GetClient().Client.GetTableReference(tableName);

            table.ExecuteAsync(TableOperation.Insert(entity));
        }

        private BaseTableEntity ComposeEntity(string message, string assembely, Level level)
        {
            return new BaseTableEntity()
            {
                LogMsg = message,
                PartitionKey = assembely,
                RowKey = Guid.NewGuid().ToString(),
                LogLevel = level.ToString()
            };
        }
    }
}
